package com.liliudong.pinkyponk.api.admin.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liliudong.pinkyponk.biz.base.BaseController;
import com.liliudong.pinkyponk.biz.sys.convert.SysRoleConvert;
import com.liliudong.pinkyponk.biz.sys.entity.SysRole;
import com.liliudong.pinkyponk.biz.sys.request.SysRoleRequest;
import com.liliudong.pinkyponk.biz.sys.response.SysRoleResponse;
import com.liliudong.pinkyponk.biz.sys.service.SysRoleService;
import com.liliudong.pinkyponk.core.validator.Create;
import com.liliudong.pinkyponk.core.validator.Update;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * SysRole
 *
 * @author liliudong
 * @date 2022-07-05 22:36
 **/
@RestController
@RequestMapping("/v1/sys/role")
public class SysRoleController extends
        BaseController<SysRole, SysRoleRequest, SysRoleResponse, SysRoleConvert, SysRoleService> {

    public SysRoleController(SysRoleService service) {
        super(SysRoleConvert.INSTANCE, service);
    }

    /**
     * create
     *
     * @param sysRoleRequest
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAuthority('sys:role:create')")
    public ResponseEntity<?> create(
            @RequestBody @Validated(Create.class) SysRoleRequest sysRoleRequest) {
        SysRole sysRole = reqToEntity(sysRoleRequest);
        service.save(sysRole);
        return ok(sysRole);
    }

    /**
     * deleteById
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:role:delete')")
    public ResponseEntity<?> deleteById(@PathVariable Integer id) {
        service.removeById(id);
        return ok();
    }

    /**
     * modifyById
     *
     * @param sysRoleRequest
     * @return
     */
    @PutMapping
    @PreAuthorize("hasAuthority('sys:role:modify')")
    public ResponseEntity<?> modifyById(
            @RequestBody @Validated(Update.class) SysRoleRequest sysRoleRequest) {
        SysRole sysRole = reqToEntity(sysRoleRequest);
        service.updateById(sysRole);
        return ok(sysRole);
    }

    /**
     * selectById
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:role:info')")
    public ResponseEntity<?> selectById(@PathVariable Integer id) {
        SysRole sysRole = service.getById(id);
        return ok(sysRole);
    }

    /**
     * selectList
     *
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public ResponseEntity<?> selectList(@RequestParam(defaultValue = "1") Long current, @RequestParam(defaultValue = "10") Long size) {
        Page<SysRole> pageQuery = getPageQuery(current, size);
        Page<SysRole> pageResult = service.page(pageQuery);
        return ok(pageResult);
    }
}
